<!DOCTYPE html>
<html ><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>网页消息通信_欲穷千里目，更上一层楼</title>
<meta name="Keywords" content="网页，网页消息通信" />
<meta name="Description" content="在浏览器中，两个不同页面（A页面的window ！= B页面的window）网页之间的消息传递。" />
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/railscasts.min.css">

<style type="text/css">
#bd{padding-top:60px;background-color:#fff;font-size:1rem;font-family:"Microsoft YaHei"; margin: 0;}
#header
{
position:fixed;
width: 100%;height: 40px;
top: 0;line-height: 40px;
padding:10px 60px;font-size: 1.5em;
box-shadow: 0 0 1px rgba(0,0,0,0.25);
transition: background-color 0.3s ease-in-out;
background-color: #fff;
z-index: 5;
}
a{text-decoration: none;}
#header a span{color: #273849;}

#main {
    position: relative;
    z-index: 1;
    padding: 0 60px 30px;
    overflow-x: hidden;
}
#main .sidebar
{
    position: fixed;
    z-index: 10;
    top: 61px;
    left: 0;
    bottom: 0;
    overflow-x: hidden;
    overflow-y: auto;
	border-right: 1px solid #eaecef;
}


.sidebar .sidebar-inner {
width: 260px;
padding: 30px 0px 60px 20px;
}
.sidebar ul {
    list-style-type: none;
    margin: 0;
    line-height: 1.5em;
    padding-left: 1em;
}
.sidebar .menu-root {
    padding-left: 0;
}
.sidebar .menu-root a{color: #4f5959;}
.sidebar a:hover {border-bottom: 2px solid #42b983;}
.sidebar .menu-root li{margin-top: 0.5em;}

.content {
    position: relative;
    padding: 2.2em 0;
    max-width: 700px;
    margin: 0 auto;
    padding-left: 50px;
}
.content h1 {margin: 0 0 1em;}
.content h2 {
    margin: 45px 0 0.8em;
    padding-bottom: 0.7em;
    border-bottom: 1px solid #ddd;
}
.content h2,
.content h3 {
  pointer-events: none;
}
.content h2 a,
.content h3 a,
.content h2 button,
.content h3 button {
  pointer-events: auto;
  color: #273849;
}
.content h2:before,
.content h3:before {
  content: "";
  display: block;
  margin-top: -91px;
  height: 91px;
  visibility: hidden;
}
.content p{ line-height: 1.6rem}
.content a {
    color: #42b983;
    font-weight: 600;
}
pre code {
    overflow-x: auto;
    color: #525252;
    white-space: pre;
    padding: 1.2em 1.4em;
    line-height: 1.6rem;
    display: block;
	font-size:1.1rem;
}

#mobile-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color: #fff;
  z-index: 9;
  display: none;
  box-shadow: 0 0 2px rgba(0,0,0,0.25);
}
#mobile-bar .menu-button {
   font-size:2.7rem; color:#757575; padding-left:1rem;float:left;}
#mobile-bar .logo { display:block;text-align:center;color:#273849;}

@media screen and (max-width: 1300px){
	.content {
		margin-left: 290px;
	}
}

@media screen and (max-width: 900px) {
#main .sidebar {
    z-index: 10;
    background-color: #f9f9f9;
    height: 100%;
    top: 0;
    left: 0;
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
    transition: all 0.4s cubic-bezier(0.4, 0, 0, 1);
    -webkit-transform: translate(-280px, 0);
    transform: translate(-280px, 0);
  }
  .sidebar .sidebar-inner {
    padding: 50px 10px 10px 20px;
    box-sizing: border-box;
  }
  .sidebar .sidebar-inner-index {
    padding: 10px 10px 10px 20px;
  }
  .sidebar .search-query {
    width: 200px;
    margin-bottom: 10px;
  }
  .sidebar .main-menu {
    display: block;
  }
  .sidebar.open {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
  }

	.content {
		margin: auto;
		 padding-left:0px
	}
	#header {
		display: none;
	}
	#mobile-bar {
		display: block;
	}
	#main .sidebar.open {
	-webkit-transform: translate(0, 0);
	transform: translate(0, 0);
	}
}

</style>
</head>
<body id="bd">

<div id="mobile-bar">
      <a class="menu-button" onClick="javascript:toggle('open')">≡</a>
      <a class="logo" href="javascript:;" ><span>网页消息通信</span></a>
      

    </div>


    <div id="header">
    <a href="javascript:;"><span>网页消息通信</span></a>
    </div>

    <div id="main">
        <div class="sidebar">
            <div class="sidebar-inner">
                <div class="list">
                    <ul class="menu-root">
                    <li><h3>导航</h3></li>    
                    <li>
                        <a href="#introduce" >介绍</a>
                    </li>   
                    <li>
                        <a href="javascript:;" class="sidebar-link">传递的方式</a>
                        <ul class="menu-sub">
                            <li><a href="#WebSocket" >WebSocket （可跨域）</a></li>
                            <li><a href="#postMessage">postMessage（可跨域）</a></li>
                            <li><a href="#SharedWorker">Worker之SharedWorker</a></li>
                            <li><a href="#Server-Sent">Server-Sent Events</a></li>
                            <li><a href="#localStorage ">localStorage </a></li>
                            <li><a href="#Cookies">Cookies</a></li>
                            <li><a href="#BroadcastChannel">BroadcastChannel(Chrome商店的api)</a></li>

                        </ul>
                    </li>  
                    </ul>
                </div>
            </div>
        </div>

        <div class="content ">
              <p>
                码云地址：
                <a href="https://gitee.com/xv700/Message-communication-for-web"  >https://gitee.com/xv700/Message-communication-for-web</a>
              </p>
                <h1 id="introduce">介绍</h1>
                <h2>
                    <a href="#Vue-js-是什么" class="headerlink" title="Vue.js 是什么" >网页消息通信是什么</a>
                </h2>
                <p>
                    即在浏览器中，两个不同页面（A页面的window ！= B页面的window）网页之间的消息传递。                
                </p>

                 <h1>
                   传递方式 
                </h1>
                <p>
                   <h2 id="WebSocket">1.WebSocket （可跨域）</h2>
                   WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。 <br>
                   <a href="https://gitee.com/xv700/websocket" target="_blank">点击进入查看使用方法</a>
                </p>
                <p>
                    <h2 id="postMessage">2.postMessage（可跨域）</h2>
                    window.postMessage()方法安全地启用Window对象之间的跨源通信。<br>
                </p>
<p>
        对将接收消息的窗口的引用，获得此类引用的方法包括：<br>
        1.iframe标签 <br>
        <a href="http://xv700.gitee.io/message-communication-for-web/postMessage/iframe/send.html"  target="_blank">DEMO在线预览</a> |     
        <a href="https://gitee.com/xv700/Message-communication-for-web/blob/master/postMessage/iframe"  target="_blank">点击进入获取源码</a>
        
        <pre>
        <code class="hljs html">//这里是发送代码 send.html
<span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">'utf-8'</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Post Message<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"width:200px; float:left;"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"color"</span>&gt;</span>Frame Color<span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"border:3px solid #0F0; width:100%; float:left"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>这里是iframe<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">iframe</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"child"</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"accept.html"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">iframe</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>&gt;</span><span class="javascript">

        <span class="hljs-built_in">window</span>.onload=<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span></span>{
            <span class="hljs-comment">//Window.frames +索引值（命名或数字）</span>
            <span class="hljs-built_in">window</span>.frames[<span class="hljs-number">0</span>].postMessage({a:<span class="hljs-number">1</span>},<span class="hljs-string">'*'</span>);

        }

        <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'message'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
            <span class="hljs-keyword">var</span> color=e.data;
            <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'color'</span>).style.backgroundColor=color;
        },<span class="hljs-literal">false</span>);
    </span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
        </code>
    </pre>
    <pre>
<code class="hljs html">//这里是接受代码 accept.html
<span class="hljs-doctype">&lt;!doctype html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">'utf-8'</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Post Message<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">style</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/css"</span>&gt;</span><span class="css">
        <span class="hljs-tag">html</span>,<span class="hljs-tag">body</span><span class="hljs-rules">{
            <span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"><span class="hljs-number">100%</span></span></span>;
            <span class="hljs-rule"><span class="hljs-attribute">margin</span>:<span class="hljs-value"><span class="hljs-number">0px</span></span></span>;
        <span class="hljs-rule">}</span></span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-title">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"height:100%;"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"container"</span> <span class="hljs-attribute">onClick</span>=<span class="hljs-value">"changeColor();"</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"widht:100%; height:100%; background-color:rgb(204, 102, 0);"</span>&gt;</span>
        click to change color
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">var</span> container=<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'container'</span>);

        <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'message'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
            <span class="hljs-built_in">console</span>.log(e);
            <span class="hljs-built_in">console</span>.info(<span class="hljs-string">"父页面传递来的消息"</span>,e.data);
            <span class="hljs-keyword">if</span>(e.source!=<span class="hljs-built_in">window</span>.parent) <span class="hljs-keyword">return</span>;
            <span class="hljs-comment">// var color=container.style.backgroundColor;</span>
            <span class="hljs-comment">// window.parent.postMessage(color,'*');//Window.parent（从嵌入式内部引用父窗口&lt;iframe&gt;）</span>
        },<span class="hljs-literal">false</span>);

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">changeColor</span> <span class="hljs-params">()</span> </span>{            
            <span class="hljs-keyword">var</span> color=container.style.backgroundColor;
            <span class="hljs-keyword">if</span>(color==<span class="hljs-string">'rgb(204, 102, 0)'</span>){
                color=<span class="hljs-string">'rgb(204, 204, 0)'</span>;
            }<span class="hljs-keyword">else</span>{
                color=<span class="hljs-string">'rgb(204,102,0)'</span>;
            }
            container.style.backgroundColor=color;
            <span class="hljs-built_in">window</span>.parent.postMessage(color,<span class="hljs-string">'*'</span>);<span class="hljs-comment">//Window.parent（从嵌入式内部引用父窗口&lt;iframe&gt;）</span>
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
            </code>
        </pre>
        2.Window.open （生成一个新窗口然后引用它） <br>
        <a href="http://xv700.gitee.io/message-communication-for-web/postMessage/window.open/"  target="_blank">DEMO在线预览</a> |     
        <a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/postMessage/window.open"  target="_blank">点击进入获取源码</a><br>
        <pre>
<code class="hljs html">//这里是发送代码 index.html
<span class="hljs-doctype">&lt;!DOCTYPE html &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>window.open<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"button"</span> <span class="hljs-attribute">onClick</span>=<span class="hljs-value">"openweb()"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"开始发送信息"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">

<span class="hljs-keyword">var</span> win=<span class="hljs-literal">null</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">openweb</span><span class="hljs-params">()</span>
</span>{
setTimeout(
        <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span>
        </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"即将发送消息：'hello world'"</span>)
            win=<span class="hljs-built_in">window</span>.open(<span class="hljs-string">'child.html'</span>,<span class="hljs-string">'child'</span>)
        }
    ,<span class="hljs-number">500</span>)
setTimeout(
        <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span>
        </span>{
            win.postMessage(<span class="hljs-string">'hello world'</span>,<span class="hljs-string">"*"</span>)
        }
    ,<span class="hljs-number">3000</span>)
}
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
                </code></pre>
        <pre>
<code class="hljs html">//这里是接受代码child.html
<span class="hljs-doctype">&lt;!DOCTYPE html &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>window.open<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"msg"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'message'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span>
</span>{
    <span class="hljs-built_in">console</span>.log(e)
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"msg"</span>).innerHTML=<span class="hljs-string">"信息来源："</span>+e.origin+<span class="hljs-string">"&lt;br&gt;"</span>+e.data

}
)
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
                </code></pre>
                
        3.Window.opener （引用产生这个的窗口） <br>
        <a href="http://xv700.gitee.io/message-communication-for-web/postMessage/window.opener/"  target="_blank">DEMO在线预览</a> |     
        <a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/postMessage/window.opener"  target="_blank">点击进入获取源码</a><br>

        <pre>
<code class="hljs html">//这里是发送代码 index.html

<span class="hljs-doctype">&lt;!DOCTYPE html &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>window.opener<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"button"</span> <span class="hljs-attribute">onClick</span>=<span class="hljs-value">"openweb()"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"开始"</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"msg"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'message'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span>
</span>{
    <span class="hljs-built_in">console</span>.log(e.origin,e.data);
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"msg"</span>).innerHTML=<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"msg"</span>).innerHTML+<span class="hljs-string">"&lt;br&gt;信息来源："</span>+e.origin+<span class="hljs-string">"&lt;br&gt;"</span>+e.data
})

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">openweb</span><span class="hljs-params">()</span></span>{
setTimeout(
    <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-built_in">window</span>.open(<span class="hljs-string">'child.html'</span>,<span class="hljs-string">'child'</span>)
    }
,<span class="hljs-number">1000</span>)}
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
                </code></pre>
        <pre>
<code class="hljs html">//这里是接受代码child.html

<span class="hljs-doctype">&lt;!DOCTYPE html &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>window.open<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"msg"</span>&gt;</span>信息已发送，请看父页面<span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">var</span> domain=<span class="hljs-string">'http://localhost:3333'</span>;

<span class="hljs-built_in">window</span>.opener.postMessage(<span class="hljs-string">"你好我是子页面"</span>,<span class="hljs-string">"*"</span>)
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
                </code></pre>
        4.Worker打开的页面 <br>
        <a href="#SharedWorker" >请看下面↓</a> 
</p>

        <p><h2 id="SharedWorker">3.SharedWorker</h2>
           1.先说一下webworker，作为浏览器的一个新特性，可以提供一个额外的线程来执行一些js代码（真正的多线程），并且不会影响到浏览器用户界面，但是不能DOM操作。<br>
            <a href="https://xv700.gitee.io/message-communication-for-web/SharedWorker/WorkerAndSharedWorker/Worker/"  target="_blank">DEMO在线预览</a> |     
            <a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/SharedWorker/WorkerAndSharedWorker/Worker"  target="_blank">点击进入获取源码</a><br>
   
            <pre>
                <code class="hljs javascript">
<span class="hljs-keyword">var</span> w=<span class="hljs-literal">null</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">startWorker</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span>(Worker)!==<span class="hljs-string">"undefined"</span>)
  {
  <span class="hljs-keyword">if</span>(w===<span class="hljs-literal">null</span>)
  {
  w=<span class="hljs-keyword">new</span> Worker(<span class="hljs-string">"demo_workers.js"</span>);
  }
  w.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(event)</span> </span>{
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=event.data;
    };
  }
<span class="hljs-keyword">else</span>
  {
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=<span class="hljs-string">"Sorry, your browser does not support Web Workers..."</span>;
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stopWorker</span><span class="hljs-params">()</span>
</span>{ 
w.terminate();
w=<span class="hljs-literal">null</span>;
}</code></pre>        


<pre>
    <code class="hljs javascript">//demo_workers.js
<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">timedCount</span><span class="hljs-params">()</span>
</span>{
i=i+<span class="hljs-number">1</span>;
postMessage(i);
setTimeout(<span class="hljs-string">"timedCount()"</span>,<span class="hljs-number">500</span>);
}

timedCount();

<span class="hljs-built_in">console</span>.log(self)//没有window对象，替代为self
<span class="hljs-built_in">console</span>.log(self.fetch)//支持fetch
<span class="hljs-built_in">console</span>.log(self.indexedDB)//支持indexedDB，不支持localStorage
</code></pre>

2.SharedWorker可以被多个window共同使用，所以可以用来跨页面传输数据，但必须保证这些标签页都是同源的(相同的协议，主机和端口号)。<br>
<a href="https://xv700.gitee.io/message-communication-for-web/SharedWorker/WorkerAndSharedWorker/SharedWorker/" target="_blank">DEMO在线预览</a> |
<a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/SharedWorker/WorkerAndSharedWorker/SharedWorker"  target="_blank">点击进入获取源码</a> 

<pre><code class="hljs javascript"><span class="hljs-doctype">&lt;!DOCTYPE HTML&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Shared workers: demo 2<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"log"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">""</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"txt"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">button</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"get"</span>&gt;</span>get<span class="hljs-tag">&lt;/<span class="hljs-title">button</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">button</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"set"</span>&gt;</span>set<span class="hljs-tag">&lt;/<span class="hljs-title">button</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
    <span class="hljs-keyword">var</span> worker = <span class="hljs-keyword">new</span> SharedWorker(<span class="hljs-string">'shared.js'</span>);
    <span class="hljs-keyword">var</span> get = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'get'</span>);
    <span class="hljs-keyword">var</span> set = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'set'</span>);
    <span class="hljs-keyword">var</span> txt = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'txt'</span>);
    <span class="hljs-keyword">var</span> log = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'log'</span>);
    worker.port.addEventListener(<span class="hljs-string">'message'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span> </span>{
    log.innerText = e.data;
    }, <span class="hljs-literal">false</span>);
    worker.port.start(); <span class="hljs-comment">// note: need this when using addEventListener</span>
    set.addEventListener(<span class="hljs-string">'click'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
        worker.port.postMessage(txt.value);
    },<span class="hljs-literal">false</span>);
    get.addEventListener(<span class="hljs-string">'click'</span>,<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(e)</span></span>{
        worker.port.postMessage(<span class="hljs-string">'get'</span>);
    },<span class="hljs-literal">false</span>);
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
        </p>
        <p>
            <h2 id="Server-Sent">4.Server-Sent Events</h2>
            HTML5 服务器发送事件（server-sent event）允许网页获得来自服务器的更新。<br>
            Server-Sent 事件指的是网页自动获取来自服务器的更新。<br>
            以前也可能做到这一点，前提是网页不得不询问是否有可用的更新。通过服务器发送事件，更新能够自动到达。<br>
<a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/Server-Sent"  target="_blank">点击进入获取源码</a> 
<pre><code class="hljs javascript"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"Content-Type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=utf-8"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Server-Sent<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>Server-Sent开始<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"result"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">h2</span>&gt;</span>Server-Sent结束<span class="hljs-tag">&lt;/<span class="hljs-title">h22</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span>(EventSource)!==<span class="hljs-string">"undefined"</span>)
    {
    <span class="hljs-keyword">var</span> source=<span class="hljs-keyword">new</span> EventSource(<span class="hljs-string">"demo_sse.asp"</span>);
    <span class="hljs-built_in">console</span>.log(source)
    source.onmessage=<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(event)</span>
    </span>{
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML+=event.data + <span class="hljs-string">"&lt;br /&gt;"</span>;
    };
    }
<span class="hljs-keyword">else</span>
    {
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"result"</span>).innerHTML=<span class="hljs-string">"抱歉，您的浏览器不支持 server-sent 事件 ..."</span>;
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
        
</code></pre>
<b>服务端</b> 事件流的对应MIME格式为text/event-stream，而且其基于HTTP长连接。针对HTTP1.1规范默认采用长连接，针对HTTP1.0的服务器需要特殊设置。<br>
<pre>
<code class="hljs javascript">//demo_sse.asp
&lt;%
Response.ContentType=<span class="hljs-string">"text/event-stream"</span>
Response.Expires=-<span class="hljs-number">1</span>
Response.<span class="hljs-function"><span class="hljs-title">Write</span><span class="hljs-params">(<span class="hljs-string">"data: "</span> &amp; now()</span></span>)
Response.<span class="hljs-function"><span class="hljs-title">Write</span><span class="hljs-params">(chr(<span class="hljs-number">13</span>)</span></span>&amp;<span class="hljs-function"><span class="hljs-title">chr</span><span class="hljs-params">(<span class="hljs-number">10</span>)</span></span>)
Response.<span class="hljs-function"><span class="hljs-title">Write</span><span class="hljs-params">(chr(<span class="hljs-number">13</span>)</span></span>&amp;<span class="hljs-function"><span class="hljs-title">chr</span><span class="hljs-params">(<span class="hljs-number">10</span>)</span></span>)
Response.<span class="hljs-function"><span class="hljs-title">Flush</span><span class="hljs-params">()</span></span>
%&gt;
</code></pre>
                </p>
                <p>
            <h2 id="localStorage">5.localStorage（可以添加事件监听）</h2>
                    localstorage是浏览器多个标签共用的存储空间，所以可以用来实现多标签之间的通信(ps：session是会话级的存储空间，每个标签页都是单独的）。 直接在window对象上添加监听即可。<br>
                    <a href="https://xv700.gitee.io/message-communication-for-web/localStorage/index.html" target="_blank">DEMO在线预览</a> |
            <a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/localStorage"  target="_blank">点击进入获取源码</a> 
            <pre>
            <code class="hljs javascript">//在同源的两个页面中，可以监听 storage 事件
            <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"storage"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(e)</span> </span>{
                alert(e.newValue);
            });
            </code></pre>
            
            <pre>
            <code class="hljs javascript">//在同一个页面中，对 localStorage 的 setItem 方法进行重写
            <span class="hljs-keyword">var</span> orignalSetItem = localStorage.setItem;
            localStorage.setItem = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(key,newValue)</span></span>{
                    <span class="hljs-keyword">var</span> setItemEvent = <span class="hljs-keyword">new</span> Event(<span class="hljs-string">"setItemEvent"</span>);
                    setItemEvent.newValue = newValue;
                    <span class="hljs-built_in">window</span>.dispatchEvent(setItemEvent);
                    orignalSetItem.apply(<span class="hljs-keyword">this</span>,<span class="hljs-built_in">arguments</span>);
            }
            <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"setItemEvent"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(e)</span> </span>{
                alert(e.newValue);
            });
            localStorage.setItem(<span class="hljs-string">"name"</span>,<span class="hljs-string">"wang"</span>);
            });
            </code></pre>
                            </p>


    <p>
        <h2 id="Cookies">6.Cookies </h2>
        Cookies在同一个域名内，并且目录也得相同，可以参考第三方库<br>
        <a href="https://github.com/js-cookie/js-cookie" target="_blank ">https://github.com/js-cookie/js-cookie</a>
    </p>

                <p>
                <h2 id="BroadcastChannel">7.BroadcastChannel(Chrome商店的api)</h2>
                这个方式，只要是在同一原始域和用户代理下，所有窗口、iframe之间都可以进行交互。这个感觉就有点类似于广播了。<br>
                <a href="https://xv700.gitee.io/message-communication-for-web/BroadcastChannel" target="_blank">DEMO在线预览</a> |
<a href="https://gitee.com/xv700/Message-communication-for-web/tree/master/BroadcastChannel"  target="_blank">点击进入获取源码</a> 
               
        <pre>
<code class="hljs javascript">//在一个页面上，触发事件，引发以下代码执行
<span class="hljs-comment">//创建一个名字是mychannel的对象。记住这个名字，下面会用到</span>
<span class="hljs-keyword">let</span> cast = <span class="hljs-keyword">new</span> BroadcastChannel(<span class="hljs-string">'mychannel'</span>); 
myObj = { <span class="hljs-keyword">from</span>: <span class="hljs-string">"children1"</span>, content: <span class="hljs-string">"add"</span> };
cast.postMessage(myObj);
</code></pre>
<pre>
    <code class="hljs javascript">//在子页面上，定义以下代码(当前页面也可以接收消息)
<span class="hljs-comment">//创建一个和刚才的名字一样的对象</span>
<span class="hljs-keyword">let</span> cast1 = <span class="hljs-keyword">new</span> BroadcastChannel(<span class="hljs-string">'mychannel'</span>);
cast1.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(e)</span> </span>{
    alert(e)
};
    </code>
</pre>
</p>

    <p> 运行后就会发现所有有上面代码的子页面都获得到了数据</p>


        </div>
   
    </div>
</body>
</html>
<script >
var sidebar=document.querySelector('.sidebar');
var menuButton = document.querySelector('.menu-button')
function toggle(aa){
	sidebar.classList.toggle(aa)
}
	
document.body.addEventListener('click', function (e) {
  if (e.target !== menuButton && !sidebar.contains(e.target)) {
	sidebar.classList.remove('open')
  }
})
</script>
<!-- 
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.8/build/highlight.min.js"></script>
<script >hljs.initHighlightingOnLoad();</script> -->